﻿
@{
    ViewBag.Title = "Map";
}

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>收货地址</title>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=0dcd72524f5aa79346017a6f84bfd325&plugin=AMap.Geocoder"></script>
    <style>
        #addressBox {
            height: 20px;
            width: 500px;
        }

        #mapBox {
            height: 400px;
            width: 630px;
        }

        #pointBox {
            height: 20px;
            width: 600px;
            margin-top: 20px;
            color: red;
        }
    </style>
</head>
<body>
    <div>
        <div style="margin-bottom:5px;">
            <input id="addressBox" type="text" placeholder="请输入结构化地址，例如北京市朝阳区望京街道首开广场" />
            <input id="searchbtn" type="button" onclick="geocoder();" value="搜索" />
            <input type="button" value="确定" onclick="SetValue();" />
            <input type="hidden" id="hidLng" />
            <input type="hidden" id="hidLat" />
        </div>

        <div id='mapBox'></div>
        <div id='pointBox'>&nbsp;</div>
    </div>
    <script type="text/javascript">
        window.onload = function () {
            map.plugin(["AMap.ToolBar"], function () {
                map.addControl(new AMap.ToolBar());
            });
            if (location.href.indexOf('&guide=1') !== -1) {
                map.setStatus({ scrollWheel: false })
            }
        }


        var $addressBox = document.getElementById('addressBox');
        var $pointBox = document.getElementById("pointBox");

        var address = GetQueryString("name");
        $addressBox.value = address;


        //创建地图
        var map = new AMap.Map("mapBox", {
            resizeEnable: true,
            center: [116.397428, 39.90923],
            zoom: 14
        });

        //添加标注
        function addMarker(point) {
            var marker = new AMap.Marker({
                map: map,
                position: [point.getLng(), point.getLat()]
            });
        }

        //地图中心点添加标注
        function addCenterPoint() {
            map.clearMap();
            var centerPoint = map.getCenter();
            addMarker(centerPoint);
            $pointBox.innerHTML = "当前经纬度为：" + centerPoint.getLng() + ',' + centerPoint.getLat();
            document.getElementById("hidLng").value = centerPoint.getLng();
            document.getElementById("hidLat").value = centerPoint.getLat();
        }
     //   addCenterPoint();

        function geocoder() {
            map.clearMap();
            var myGeo = new AMap.Geocoder();
            //地理编码,返回地理编码结果
            myGeo.getLocation($addressBox.value, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    //地址解析成功
                    geocoder_CallBack(result);
                }
                else {
                    //地址解析失败
                    $pointBox.innerHTML = "查无此地址";
                }
            });
        }

        //地理编码返回结果展示
        function geocoder_CallBack(data) {
            var resultStr = "";
            var geocode = data.geocodes;
            addMarker(geocode[0].location);
            resultStr += "当前坐标</b>：" + geocode[0].location.getLng() + ", " + geocode[0].location.getLat();
            map.setFitView();
            $pointBox.innerHTML = resultStr;
        }

        map.on('moveend', function () {
            addCenterPoint();
        });

        function GetQueryString(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");

            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;

        }

        function SetValue()
        {
            var lng = document.getElementById("hidLng").value;
            var lat = document.getElementById("hidLat").value;
            if (lng == "" || lat == "") {
                alert("请获取坐标!");
                return;
            } else {
                parent.SetLocation(lng, lat);
                parent.layer.closeAll();
            }

        }
        document.getElementById("searchbtn").onclick();
    </script>
</body>
</html>